package me.zhangyifeng.blog.web.controller.front;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import me.zhangyifeng.blog.model.domain.Activity;
import me.zhangyifeng.blog.model.domain.User;
import me.zhangyifeng.blog.model.dto.JsonResult;
import me.zhangyifeng.blog.model.dto.MaydayConst;
import me.zhangyifeng.blog.service.ActivityService;
import me.zhangyifeng.blog.web.controller.admin.BaseController;

/**
 * 前台活动控制器
 * 
 * @author zhangyifeng
 */
@Controller
public class ActivityController extends BaseController {
    
    @Autowired
    private ActivityService activityService;
    
    /**
     * 活动列表页
     */
    @GetMapping("/activities")
    public String activities(Model model, HttpServletRequest request) {
        // 获取当前登录用户
        User user = (User) request.getSession().getAttribute(MaydayConst.USER_SESSION_KEY);
        Integer userId = user != null ? user.getUserId() : null;
        
        // 查询所有活动（带参与信息）
        List<Activity> activities = activityService.findAllActivitiesWithParticipationInfo(userId);
        model.addAttribute("activities", activities);
        
        return this.render("activities");
    }
    
    /**
     * 活动详情页
     */
    @GetMapping("/activities/{activityId}")
    public String activityDetail(@PathVariable("activityId") Integer activityId, Model model, HttpServletRequest request) {
        // 获取当前登录用户
        User user = (User) request.getSession().getAttribute(MaydayConst.USER_SESSION_KEY);
        Integer userId = user != null ? user.getUserId() : null;
        
        // 查询活动详情（带参与信息）
        Activity activity = activityService.findActivityWithParticipationInfo(activityId, userId);
        if (activity == null) {
            return this.render_404();
        }
        
        model.addAttribute("activity", activity);
        
        // 查询参与者列表
        model.addAttribute("participants", activityService.findParticipantsByActivityId(activityId));
        
        return this.render("activity_detail");
    }
    
    /**
     * 参与活动（Ajax请求）
     */
    @PostMapping("/activities/join/{activityId}")
    @ResponseBody
    public JsonResult joinActivity(@PathVariable("activityId") Integer activityId, HttpSession session) {
        // 获取当前登录用户
        User user = (User) session.getAttribute(MaydayConst.USER_SESSION_KEY);
        if (user == null) {
            return new JsonResult(false, "请先登录");
        }
        
        return activityService.joinActivity(user.getUserId(), activityId);
    }
    
    /**
     * 退出活动（Ajax请求）
     */
    @PostMapping("/activities/quit/{activityId}")
    @ResponseBody
    public JsonResult quitActivity(@PathVariable("activityId") Integer activityId, HttpSession session) {
        // 获取当前登录用户
        User user = (User) session.getAttribute(MaydayConst.USER_SESSION_KEY);
        if (user == null) {
            return new JsonResult(false, "请先登录");
        }
        
        return activityService.quitActivity(user.getUserId(), activityId);
    }
    
    /**
     * 我参与的活动
     */
    @GetMapping("/user/my-activities")
    public String myActivities(Model model, HttpSession session) {
        // 获取当前登录用户
        User user = (User) session.getAttribute(MaydayConst.USER_SESSION_KEY);
        if (user == null) {
            return "redirect:/user/login";
        }
        
        // 查询用户参与的活动
        List<Activity> activities = activityService.findActivitiesByUserId(user.getUserId());
        model.addAttribute("activities", activities);
        
        return "user/my_activities";
    }
}
